<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>

<%@ taglib uri="/WEB-INF/hexacta.tld" prefix="hexacta"%>

<%@ page import="org.vectrics.hibernate.ThreadProperties"%>
<%@ page import="org.vectrics.domain.Domain"%>
<%@ page import="java.util.Collection"%>
<%@ page import="java.util.List"%>
<%@ page import="org.vectrics.recruiter.RecruitServiceLocator"%>


<%@page import="org.vectrics.cv.Skill"%>
<%@page import="org.vectrics.cv.SkillType"%>
<%@page import="org.vectrics.cv.Level"%>
<%@page import="org.vectrics.provider.CoreServiceLocator"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.collections.Predicate"%>
<%@page import="org.apache.commons.collections.CollectionUtils"%>
<%@page import="org.vectrics.cv.PersonSkill"%>
<%@page import="org.vectrics.user.Person"%>
<%@page import="org.vectrics.cv.ExtraCertificate"%>
<%@page import="java.util.ArrayList"%>

<%@page import="org.vectrics.recruiter.struts.tag.cv.SkillsViewTag"%>
<%@page import="org.vectrics.cv.ListableSkill"%>
<%@page import="java.util.SortedMap"%>
<%@page import="java.util.SortedSet"%>
<%@page import="java.util.TreeSet"%>
<%@page import="java.util.Set"%><script language="JavaScript"
	type="text/javascript"
	src="/hrs-intranet/js/dynamicTree/DynamicTree.js"></script>
<script language="JavaScript" type="text/javascript"
	src="/hrs-intranet/js/dynamicTree/ie5.js"></script>
<link rel="stylesheet" type="text/css"
	href="/hrs-intranet/js/dynamicTree/DynamicTree.css" />


<%
    String contextPath = request.getContextPath();

    // Recupero la lista de todos los skills padres
    Collection<Skill> parentSkillsAux = CoreServiceLocator.getPersonService().findAllParentSkill();
    Set<Skill> parentSkills = new TreeSet<Skill>(parentSkillsAux);

    // Recupero los levels
    Collection<Level> levels = CoreServiceLocator.getPersonService().findAllLevels();

    Skill parent = null;

    for (Skill s : parentSkillsAux) {
        if (s.getName().equals("Certificaciones de Microsoft")) {
            parent = s;
            break;
        }
    }
    
    request.setAttribute("parentSkills", parentSkills);
    request.setAttribute("levels", levels);

%>

<head>
<sx:head cache="true" />
<link rel="stylesheet" href="/hrs-intranet/styles/displaytag.css"
	type="text/css" />
</head>
<s:include value="errorsSection.jsp"></s:include>
<s:hidden name="preRequirements" id="preRequirements" />
<s:form cssClass="formClass">
	<s:hidden id="requirementProjectId" name="requirementProjectId" />
	<s:hidden id="requirementProjectName" name="requirementProjectName" />
	<s:textfield label="Project" name="requirementProjectName"
		disabled="true" cssClass="dojoComboBox" />
	<sx:autocompleter id="roles" list="roles" listValue="description"
		searchType="startword" listKey="code" showDownArrow="false"
		keyName="requirementRoleCode" name="requirementRoleName"
		label="Assigned Role" />
	<sx:datetimepicker id="fromDate" name="requirementFromDate"
		label="Start Date" displayFormat="dd/MM/yyyy" />
	<sx:datetimepicker id="toDate" name="requirementToDate"
		label="End Date" displayFormat="dd/MM/yyyy" />
	<s:textfield label="Percentage (% FTE)" name="requirementPercentage"
		cssClass="dojoComboBox" />
	
	<table >
		<tr align="left">
			<th colspan="2"><label class="label">Skills:</label>
			</th>
		</tr>
		<tr>
			<td valign="top" colspan="2"><jsp:useBean id="skillForm"
					class="org.vectrics.recruiter.struts.cv.SkillForm" scope="request" />
				<table width="500">
					<tr>
						<td valign="middle">
							<div class="DynamicTree">
								<div class="wrap" id="tree">

									<%
									    // recorremos los skills padres - SKP1
									        // Los skill se cargan solo en 3 niveles como maximo.
									        for (Skill parentSkill : parentSkills) {

									            if ((parentSkill.getChildSkills() != null) && !parentSkill.getChildSkills().isEmpty()) {
									%>
									<div class="folder">
										<input type="checkbox" name="padre" value=""
											disabled="disabled" /><%=parentSkill.getName()%>

										<%
										    // recorremos los skills hijos - SKC1

										                Collection<Skill> skillsSubCategoriaAux = parentSkill.getChildSkills();
										                Set<Skill> skillsSubCategoria = new TreeSet<Skill>(skillsSubCategoriaAux);
										                for (Skill skill : skillsSubCategoria) {
										                    // si no tiene hijos lo dibujo
										                    if ((skill.getChildSkills() == null) || skill.getChildSkills().isEmpty()) {
									                            if (skill.getType() == SkillType.LEVELED_TYPE) {
										%>
										<div class="doc">
											<input type="checkbox" name="skill_<%=skill.getId()%>"
												value="<%=skill.getId()%>" /><%=skill.getName()%>
											<input type="hidden" id="l_<%=skill.getId()%>" value="0" />
										</div>

										<%
										    } else {
										%>
										<div class="doc boolean">
											<input type="checkbox" name="skill_<%=skill.getId()%>"
												value="<%=skill.getId()%>" /><%=skill.getName()%>
										</div>
										<%
										    }
										                    } else {
										                        //Si tiene hijos lo muestro y muestro sus hijos
										%>
										<div class="folder">
											<input type="checkbox" name="padre" value=""
												disabled="disabled" /><%=skill.getName()%>
											<%
											    // recorremos los skills hijos - SKC1

											                        Collection<Skill> skillsAux = skill.getChildSkills();
											                        Set<Skill> skills = new TreeSet<Skill>(skillsAux);
											                        for (Skill child : skills) {
											                            // buscamos si el skill hijo lo tiene la persona para poder checkearlo o no
											%>
											<div class="doc">
												<input type="checkbox" name="skill_<%=child.getId()%>"
													value="<%=child.getId()%>" /><%=child.getName()%>
												<input type="hidden" id="l_<%=child.getId()%>" value="0" />
											</div>
											<%
											    }
											%>
										</div>
										<%
										    }
										                } // END SKC1
										%>
									</div>
									<%
									    } // END IF
									        } // END SKP1
									%>
								</div>
							</div></td>
					</tr>
				</table>
				<div style="display: none;" id="divLevel">
					<select name="level" id="level">
						<%
					    // recorremos y armamos los options del combo level
					    for (Level level : levels) {
					%>
						<option value="<%=level.getId()%>"
						<% if (level.getName().equalsIgnoreCase("Intermediate")){ %>
							selected="selected"
							<% }%>
						><%=level.getName()%></option>
						<%
					}
					%>
					</select>
				</div> <script type="text/javascript">
					var tree = new DynamicTree("tree");
					tree.init();
				</script>
			</td>
		</tr>
		<tr align="center" valign="top">
			<td>
				<s:submit key="Add" cssClass="btn" id="addbtn"
					action="RequirementAction_addPreRequirement" />
			</td>
			<td>
				<s:submit key="Cancel" cssClass="btn" id="addbtn"
					action="RequirementAction_filterRequirement" />
			</td>
		</tr>
	</table>

	
	<s:if
		test="%{!preRequirements.isEmpty() || !employeeAssignmentList.size()==0}">
		<table width="67%">
			<tr>
				<td colspan="2"><display:table id="preRequirements"
						name="preRequirements" defaultsort="1" defaultorder="descending"
						requestURI="RequirementAction_createRequirement" class="listable"
						cellpadding="0" cellspacing="0" pagesize="10">
						<display:column property="startDate.time" title="Start Date"
							sortable="true" format="{0,date,dd/MM/yyyy}" />
						<display:column property="endDate.time" title="End Date"
							sortable="true" format="{0,date,dd/MM/yyyy}" />
						<display:column property="assignmentPorcentage" title="% FTE"
							sortable="true" format="{0,number,00} %" />
					</display:table>
				</td>
			</tr>
			<tr>
				<s:submit key="Confirm" cssClass="btn" id="confirmbtn"
					action="RequirementAction_confirmCreateRequirements" />
			</tr>
		</table>
	</s:if>
</s:form>